<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace hdphp\services\wechat;

use hdphp\services\wechat\contract\BaseApplicationInterface;
use think\facade\Log;

/**
 * Class：BaseApplication
 * 创建人：Jack
 * 创建时间：2023/11/30 11:08
 * @package hdphp\services\wechat
 */
abstract class BaseApplication implements BaseApplicationInterface
{
    // app端
    const APP = 'app';
    // h5端、公众端
    const WEB = 'web';
    // 小程序端
    const MINI = 'mini';
    // 开发平台
    const OPEN = 'open';
    // pc端
    const PC = 'pc';

    /**
     * 访问端
     * @var string
     */
    protected $accessEnd;

    /**
     * @var array
     */
    protected static $property = [];

    /**
     * @var string
     */
    protected $pushMessageHandler;

    /**
     * Debug
     * @var bool
     */
    protected $debug = true;

    /**
     * 设置消息处理类
     * @param string $handler
     * @return $this
     */
    public function setPushMessageHandler(string $handler)
    {
        $this->pushMessageHandler = $handler;
        return $this;
    }

    /**
     * 设置访问端
     * @param string $accessEnd
     * @return $this
     */
    public function setAccessEnd(string $accessEnd)
    {
        if (in_array($accessEnd, [self::APP, self::WEB, self::MINI])) {
            $this->accessEnd = $accessEnd;
        }
        return $this;
    }

    /**
     * 注释：自动获取访问端
     * 创建人：Jack
     * 创建时间：2023/12/11 14:53
     * @param \think\Request $request
     * @return string
     */
    public function getAuthAccessEnd(\think\Request $request)
    {
        if (!$this->accessEnd) {
            try {
                if ($request->isApp()) {
                    $this->accessEnd = self::APP;
                } else if ($request->isPc()) {
                    $this->accessEnd = self::PC;
                } else if ($request->isWechat() || $request->isH5()) {
                    $this->accessEnd = self::WEB;
                } else if ($request->isRoutine()) {
                    $this->accessEnd = self::MINI;
                } else {
                    $this->accessEnd = self::WEB;
                }
            } catch (\Throwable $e) {
                $this->accessEnd = self::WEB;
            }
        }
        return $this->accessEnd;
    }

    /**
     * 记录错误日志
     * @param \Throwable $e
     */
    protected static function error(\Throwable $e)
    {
        static::instance()->debug && Log::error([
            'error' => $e->getMessage(),
            'line' => $e->getLine(),
            'file' => $e->getFile()
        ]);
    }

    /**
     * 请求日志
     * @param string $message
     * @param        $request
     * @param        $response
     */
    protected static function logger(string $message, $request, $response)
    {
        $debug = static::instance()->debug;

        if ($debug) {
            Log::info([
                'message' => $message,
                'request' => json_encode($request),
                'response' => json_encode($response)
            ]);
        }
    }

    /**
     * 注释：__callStatic
     * 创建人：Jack
     * 创建时间：2023/11/30 11:11
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public static function __callStatic($name, $arguments)
    {
        if (in_array($name, array_keys(static::$property))) {
            $name = static::$property[$name];
            return static::instance()->application()->{$name};
        }
        throw new WechatException('方法不存在!');
    }
}